<template>
  <div class="sidebar">
    <div class="logo">
      <div class="logo-icon">
        <img :src="logoPath" alt="GuLu" />
      </div>
      <h2>GuLu <span>Client</span></h2>
    </div>
    
    <el-menu
      :default-active="activeMenu"
      class="sidebar-menu"
      background-color="transparent"
      text-color="#cbd5e1"
      active-text-color="#60a5fa"
      @select="handleMenuSelect"
    >
      <el-menu-item index="/connections" class="menu-item">
        <i class="el-icon-link"></i>
        <span>{{ $t('sidebar.connections') }}</span>
      </el-menu-item>
      
      <el-menu-item index="/explorer" :disabled="!isConnected" class="menu-item">
        <i class="el-icon-files"></i>
        <span>{{ $t('sidebar.explorer') }}</span>
      </el-menu-item>
      
      <el-menu-item index="/query" :disabled="!isConnected" class="menu-item">
        <i class="el-icon-edit"></i>
        <span>{{ $t('sidebar.query') }}</span>
      </el-menu-item>
      
      <el-menu-item index="/visualization" :disabled="!hasResults" class="menu-item">
        <i class="el-icon-data-analysis"></i>
        <span>{{ $t('sidebar.visualization') }}</span>
      </el-menu-item>
      
      <el-menu-item index="/settings" class="menu-item">
        <i class="el-icon-setting"></i>
        <span>{{ $t('sidebar.settings') }}</span>
      </el-menu-item>
      
      <el-menu-item index="/about" class="menu-item">
        <i class="el-icon-info"></i>
        <span>{{ $t('sidebar.about') }}</span>
      </el-menu-item>
    </el-menu>
    
    <div class="connection-status">
      <div class="status-card">
        <div class="status-indicator" :class="isConnected ? 'connected' : 'disconnected'"></div>
        <div class="status-text">
          <span class="status-label">Status</span>
          <span class="status-value">{{ connectionStatus }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters, mapState } from 'vuex'

export default {
  name: 'Sidebar',

  data() {
    return {
      logoPath: require('@/assets/icon.jpg')
    }
  },

  computed: {
    ...mapState(['currentConnection', 'queryResults']),
    ...mapGetters(['isConnected']),

    activeMenu() {
      return this.$route.path
    },

    connectionStatus() {
      if (this.isConnected) {
        return `${this.currentConnection.name || this.currentConnection.url}`
      }
      return 'Not Connected'
    },

    hasResults() {
      return this.queryResults && this.queryResults.length > 0
    }
  },
  
  methods: {
    handleMenuSelect(index) {
      // Don't navigate if already on the same route
      if (this.$route.path === index) {
        return
      }
      
      if (index === '/explorer' && !this.isConnected) {
        this.$message.warning('Please connect to a database first')
        return
      }
      
      if (index === '/query' && !this.isConnected) {
        this.$message.warning('Please connect to a database first')
        return
      }
      
      if (index === '/visualization' && !this.hasResults) {
        this.$message.warning('Please execute a query first')
        return
      }
      
      this.$router.push(index).catch(err => {
        // Ignore the navigation duplicate error
        if (err.name !== 'NavigationDuplicated') {
          console.error(err)
        }
      })
    }
  }
}
</script>

<style scoped>
.sidebar {
  height: 100%;
  display: flex;
  flex-direction: column;
  background: transparent;
  overflow: hidden;
}

.logo {
  padding: 30px 20px;
  text-align: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.logo-icon {
  width: 40px;
  height: 40px;
  border-radius: 999px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  box-shadow: 0 4px 6px rgba(102, 126, 234, 0.3);
}

.logo-icon img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.logo h2 {
  color: #f1f5f9;
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.5px;
}

.logo h2 span {
  font-weight: 300;
  color: #94a3b8;
}

.sidebar-menu {
  flex: 1;
  border-right: none;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 20px 15px;
}

.menu-item {
  margin-bottom: 8px;
  border-radius: 8px;
  transition: all 0.3s;
}

.menu-item:hover {
  background: rgba(255, 255, 255, 0.05) !important;
}

.menu-item.is-active {
  background: rgba(96, 165, 250, 0.1) !important;
  border-left: 3px solid #60a5fa;
}

.menu-item i {
  margin-right: 10px;
  font-size: 18px;
}

.menu-item span {
  font-size: 14px;
  font-weight: 500;
}

.connection-status {
  padding: 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.status-card {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 10px;
  padding: 15px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.status-indicator {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  animation: pulse 2s infinite;
}

.status-indicator.connected {
  background: #10b981;
  box-shadow: 0 0 10px rgba(16, 185, 129, 0.5);
}

.status-indicator.disconnected {
  background: #94a3b8;
  animation: none;
}

.status-text {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.status-label {
  font-size: 11px;
  color: #94a3b8;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.status-value {
  font-size: 13px;
  color: #e2e8f0;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.4);
  }
  70% {
    box-shadow: 0 0 0 10px rgba(16, 185, 129, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(16, 185, 129, 0);
  }
}
</style>